import { Component, OnInit, ViewChild } from '@angular/core';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { JsonApiService } from "app/core/api/json-api.service";

declare var $: any;
@Component({
	selector: 'sa-sensor-deploy',
	templateUrl: './sensor-deploy.component.html',
	styleUrls: ['./sensor-deploy.component.css']
})
export class SensorDeployComponent implements OnInit {
	tableData: any = []; // table的表单数据
	totalCounts: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //内置的分页每页条数
	activeSelected: string = ''; //row高亮
	// 查询参数
	searchKey:any=""; // 用户的搜索关键字

	// 标志是否选择车辆的
	isShowDelete: any = true; //是否有删除权限
	// 是编辑还是新增的
	formType: any = 'add';
	startTime: any = '';
	endTime: any = '';
	// 表单验证
	validatorOptions = {
		excluded: [':disabled'],
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
			sensorName: {
				validators: {
					notEmpty: {
						message: '请填写传感器名称'
					},
				}
			},
			sensorCode: {
				validators: {
					notEmpty: {
						message: '请填写传感器编码'
					},
				}
			},
		}
	}
	constructor(private router: Router, private activatedRoute: ActivatedRoute, private jsonApiService: JsonApiService) {
		activatedRoute.queryParams.subscribe(queryParams => {
		});
	}
	ngOnInit() {
		// this.initBtn();
		// this.initDate();
		this.load();
	}
	//初始化权限按钮
	initBtn() {
		var btnData = JSON.parse(decodeURIComponent(localStorage.getItem('cargpsw_system:sensorDeploy')));
		if (!btnData) {
			return
		};
		for (let item of btnData) {
			if (item.sourceKey === "system:sensorDeploy:deleteBtn" && item.parentId) {
				this.isShowDelete = true;
			}

		}
	}
	initDate(){
		var that = this;
		return new Promise(function (resolve, reject) {
			System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(()=>{
					System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(()=>{
						$("#datetimepicker1").datetimepicker({//时间插件
							format: "yyyy-mm-dd hh:ii:ss",
						    language: "zh-CN",
						    autoclose: true,
						}).on("changeDate", function(e){
							var date = e.date;
							var time = common.getTimeMmss(date);
							$("#datetimepicker2").datetimepicker("setStartDate", time);
							that.startTime=time;
						});
						$("#datetimepicker2").datetimepicker({//时间插件
							format: "yyyy-mm-dd hh:ii:ss",
						    language: "zh-CN",
						    autoclose: true,
						}).on("changeDate", function(e){
							var date = e.date;
							var time = common.getTimeMmss(date);
							$("#datetimepicker1").datetimepicker("setEndDate", time);
							that.endTime=time;
						});

						resolve();
					});
				}
			);
		})

	}
	load() {
		// this.startTime =$('#datetimepicker1').val();
		// this.endTime=$('#datetimepicker2').val();
		// if(this.startTime==''&&this.endTime!==''){
		// 	this.jsonApiService.message('起始时间不能为空');
		// 	return;
		// }
		// if(this.startTime!==''&&this.endTime==''){
		// 	this.jsonApiService.message('结束时间不能为空')
		// 	return;
		// }
		// if(this.startTime!==''&& !common.checkDateFormat(this.startTime)){
		// 	this.jsonApiService.message('起始时间格式错误')
		// 	return;
		// }
		// if(this.endTime!==''&& !common.checkDateFormat(this.endTime)){
		// 	this.jsonApiService.message('结束时间格式错误')
		// 	return;
		// }
		let param = {
			"pageSize": this.pageSize,
			"nowPage": this.curPage,
			"parameters": {
				searchKey:this.searchKey,
				// startTime: this.startTime,
				// endTime: this.endTime
			}
		}
		let reqdata = 'gridPager=' + JSON.stringify(param);
		this.jsonApiService.http("sensorManage/sensorManagementForPage.html", reqdata).subscribe(result => {
			let data = []
			if (result.isSuccess && result.exhibitDatas) {
				data = result.exhibitDatas
				this.tableData = result.exhibitDatas
				this.totalCounts = result.recordCount;
			}
		});
	}
	// 分页回调
	paginate(e) {
		this.activeSelected = '';
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		this.load();
	}
	// 刷新表
	refresh() {
		this.load();
	}
	// 删除续费记录
	del_domCode(row) {
		this.jsonApiService.confirm_message('此操作将永久删除数据,确认要删除').then(() => {
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("sensorManage/deleteById.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.load();
				}
			})
		})
	}
	// 表单查询
	tableSearch() {
		this.curPage = 1;
		this.load();
	}

	// 限制进厂的配置提交对象
	sensorDeployObj: any = {
		sensorName: "",
		sensorCode: '',
	};
	// 

	// 添加续费记录的弹窗
	showAddLimitModal(modal) {
		this.formType = 'add'
		modal.show();
		this.resetForm();
	}
	// 重置添加表
	resetForm() {
		this.sensorDeployObj = {
			sensorName: "",
			sensorCode: '',
		};
		//初始化表单
		$("#sensorDeploy").data('bootstrapValidator').resetForm();
	}
	// 关闭添加弹框
	closesensorDeployModal(modal) {
		$("#sensorDeploy").data('bootstrapValidator').resetForm();
		modal.hide();
	}
	// 提交添加结果
	submitModal(modal) {
		$('#sensorDeploy').bootstrapValidator('validate'); //开启验证
		let isValid = $('#sensorDeploy').data('bootstrapValidator').isValid();  //验证是否通过
		if (isValid) {
			let reqdata = this.sensorDeployObj;
			var url;
			if(this.formType == 'add'){
				url = "sensorManage/addSensor.html"
			}else{
				url = "sensorManage/editSensor.html"
			}
			this.jsonApiService.http_obj(url, reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.load();
					this.closesensorDeployModal(modal)
				}
			})
		}
	}
	// 编辑
	editRow(row, modal) {
		modal.show();
		setTimeout(() => {
			this.formType = 'edit'
			this.sensorDeployObj = JSON.parse(JSON.stringify(row))
		}, 300);
		$("#sensorDeploy").data('bootstrapValidator').resetForm();
	}
}